import { Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { InjectModel } from '@nestjs/sequelize';

import { Submitter } from '@/models/submitter.model';
import { CreateSubmissionDto } from '@/modules/submission/dto/create-submission.dto';
import { ListTaskDto } from '@/modules/submission/dto/list.submission.dto';
import { UpdateSubmissionDto } from '@/modules/submission/dto/update-submission.dto';
import { responseMessage } from '@/utils';

@Injectable()
export class SubmissionService {
  constructor(
    @InjectModel(Submitter)
    private readonly submissionModel: typeof Submitter,
    private readonly jwtService: JwtService,
  ) {}
  create(createSubmissionDto: CreateSubmissionDto) {
    return 'This action adds a new submission';
  }

  async findAllTask(taskList: ListTaskDto) {
    const { current = 1, pageSize = 10 } = taskList;
    const result = await this.submissionModel.findAll({
      offset: (current - 1) * pageSize,
      limit: pageSize,
    });
    const total = await this.submissionModel.count();
    if (result.length) {
      return responseMessage({ list: result, total, current, pageSize });
    }
    return responseMessage({ list: result, total, current, pageSize }, '暂无数据');
  }

  async findUserAllTask(token: string) {
    const { user_id } = this.jwtService.decode(token.split(' ').pop());
    const result = await this.submissionModel.findAll({ where: { user_id } });
    if (result.length === 0) {
      return responseMessage(result, '暂无数据');
    }
    return responseMessage(result);
  }

  findOne(id: number) {
    return `This action returns a #${id} submission`;
  }

  update(id: number, updateSubmissionDto: UpdateSubmissionDto) {
    return `This action updates a #${id} submission`;
  }

  remove(id: number) {
    return `This action removes a #${id} submission`;
  }
}
